package com.example.dingdong.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

/**
 * 
 * @ClassName: TokenTools
 * @Description: 
 * @Author :chengwenixng
 * @Copyright: Smartlab411
 * @DateTime 2021-07-13 14:42:10
 */
public class TokenTools {
	public static String createToken(String appId, String appKey, String channelId, String userId, String nonce,
			Long timestamp) throws NoSuchAlgorithmException {
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
		digest.update(appId.getBytes());
		digest.update(appKey.getBytes());
		digest.update(channelId.getBytes());
		digest.update(userId.getBytes());
		digest.update(nonce.getBytes());
		digest.update(Long.toString(timestamp).getBytes());

		String token = DatatypeConverter.printHexBinary(digest.digest()).toLowerCase();
		return token;
	}

	public static String createUserID(String channelID, String user) throws NoSuchAlgorithmException {
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
		digest.update(channelID.getBytes());
		digest.update("/".getBytes());
		digest.update(user.getBytes());

		String uid = DatatypeConverter.printHexBinary(digest.digest()).toLowerCase();
		return uid.substring(0, 16);
	}
}
